﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FIM
{
    public partial class FormCapture : Form
    {
        public FormCapture()
        {
            InitializeComponent();
            btCapture.Focus();
        }

        public void cmdWmpCapPlay(string ip)
        {
            if (wmpCap.playState == WMPLib.WMPPlayState.wmppsPlaying)
                return;
            wmpCap.network.bufferingTime = 1000;
            wmpCap.URL = "mms://" + ip + ":5004";
            wmpCap.Ctlcontrols.play();
        }

        public void cmdWmpCapStop()
        {
            wmpCap.Ctlcontrols.stop();
        }


        private FormMain root;
        private Bitmap bScreen = null;
        private Graphics gScreen = null;

        public void setRoot(FormMain r)
        { root = r; }

        private void captureVideoFrame()
        {
            int w = wmpCap.Width;
            int h = wmpCap.Height;

            if (bScreen == null)
                bScreen = new Bitmap(w, h);
            if (gScreen == null)
                gScreen = Graphics.FromImage(bScreen);

            gScreen.CopyFromScreen(wmpCap.PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(w, h));

            if (root != null)
                root.setScreenshot(bScreen);
            this.Hide();
        }

        private void btCapture_Click(object sender, EventArgs e)
        {
            this.captureVideoFrame();
        }

        private void wmpCap_KeyPressEvent(object sender, AxWMPLib._WMPOCXEvents_KeyPressEvent e)
        {
            if (e.nKeyAscii == (short)Keys.Escape)
                this.Hide();
            else if (e.nKeyAscii == (short)Keys.Enter)
                this.captureVideoFrame();

        }

    }
}
